using System;
using System.Drawing;
using System.Windows.Forms;

namespace HyperbolaControl
{
    public class NumericDisplayControl : Control
    {
        private string _name = "名称";
        private string _unit = "单位";
        private float _value = 0;
        private float _rangeMin = 0;
        private float _rangeMax = 100;
        private Color _valueColor = Color.Lime;
        
        public string DisplayName 
        { 
            get => _name; 
            set { _name = value; Invalidate(); } 
        }
        
        public Color ValueColor
        {
            get => _valueColor;
            set { _valueColor = value; Invalidate(); }
        }
        
        public string Unit 
        { 
            get => _unit; 
            set { _unit = value; Invalidate(); } 
        }
        
        public float Value 
        { 
            get => _value; 
            set { _value = value; Invalidate(); Refresh(); } 
        }
        
        public float RangeMin 
        { 
            get => _rangeMin; 
            set { _rangeMin = value; } 
        }
        
        public float RangeMax 
        { 
            get => _rangeMax; 
            set { _rangeMax = value; } 
        }
        
        public NumericDisplayControl()
        {
            this.DoubleBuffered = true;
            this.BackColor = Color.White;
            this.ForeColor = Color.Black;
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            
            // 清除整个控件区域
            g.Clear(this.BackColor);
            
            // 绘制圆角矩形背景
            using (var path = GetRoundedRectanglePath(this.ClientRectangle, 15))
            {
                g.FillPath(new SolidBrush(Color.Black), path);
                g.DrawPath(new Pen(Color.White, 1), path);
            }
            
            // 绘制名称（紧贴左上角）
            using (var nameFont = new Font(this.Font.FontFamily, 12))
            {
                TextRenderer.DrawText(g, _name, nameFont, 
                    new Point(2, 2), Color.White);
            }
                
            // 绘制单位（紧贴右下角）
            using (var unitFont = new Font(this.Font.FontFamily, 10))
            {
                var unitSize = TextRenderer.MeasureText(g, _unit, unitFont);
                TextRenderer.DrawText(g, _unit, unitFont, 
                    new Point(this.Width - unitSize.Width - 2, this.Height - unitSize.Height - 2), 
                    Color.White);
            }
                
            // 绘制数值（中间靠下）
            using (var valueFont = new Font(this.Font.FontFamily, 20))
            {
                var valueText = _value.ToString("F2");
                var valueSize = TextRenderer.MeasureText(g, valueText, valueFont);
                TextRenderer.DrawText(g, valueText, valueFont, 
                    new Point((this.Width - valueSize.Width) / 2, (this.Height - valueSize.Height) / 2 + 10), 
                    _valueColor);
            }
        }
        
        private System.Drawing.Drawing2D.GraphicsPath GetRoundedRectanglePath(Rectangle rect, int radius)
        {
            var path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
            path.AddArc(rect.Right - radius, rect.Y, radius, radius, 270, 90);
            path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
            path.AddArc(rect.X, rect.Bottom - radius, radius, radius, 90, 90);
            path.CloseFigure();
            return path;
        }
    }
}